package com.study;

import com.study.configuration.SpringConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestIoc {

    public static void main(String[] args) {
        //testXml();
        //testXmlAndAnnotation();
        //testAnnotation();
    }

    /*xml方式*/
    public static void testXml() {
        /**
         * 步骤：1、只需在xml中配置bean即可
         */
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-xml.xml");
        printAllBeans(context);
    }

    /*xml和annotation混合方式*/
    public static void testXmlAndAnnotation() {
        /**
         * 步骤：1、开启注解，并扫描指定包下带有注解的类 <context:component-scan base-package=""/>
         *      2、在类上加上@Component注解，或者他的衍生注解@Service、@Comtroller、@Repository
         */
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-xml-annotation.xml");
        printAllBeans(context);
    }

    /*纯注解方式*/
    public static void testAnnotation() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        printAllBeans(context);
    }

    /*打印出所有的bean*/
    public static void printAllBeans(ApplicationContext context) {
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(context.getBean(name));
        }
    }

}
